﻿using System;

namespace SmlEngine.IO.Uri
{
    internal static class AdvancedUriHelper
    {
        #region Constants

        private const string Base = "*B";
        private const string Containing = "*U";
        private const string Content = "*C";
        private const string Relative = "*R";

        #endregion

        /// <summary>
        /// Returns an AdvancedUri from a specified string, complete with the correct AdvancedUriKind.
        /// </summary>
        /// <param name="uriString">The string to create the AdvancedUri from.</param>
        public static AdvancedUri RetrieveUri(string uriString)
        {
            AdvancedUriKind kind = RetrieveUriType(uriString);
            string path = RemoveIdentifier(uriString);

            AdvancedUri uri = new AdvancedUri(path, kind);
            return uri;
        }

        /// <summary>
        /// Returns the AdvancedUriKind contained in a specified string. 
        /// If none exist in the string, AdvancedUriKind.Indeterminate is returned.
        /// </summary>
        /// <param name="uriString">The string to retrieve the AdvancedUriKind from.</param>
        public static AdvancedUriKind RetrieveUriType(string uriString)
        {
            uriString = uriString.Trim();
            AdvancedUriKind kind;

            if (uriString.Length < 2)
                return AdvancedUriKind.Indeterminate;

            switch (uriString.Substring(0, 2))
            {
                case Base:
                    kind = AdvancedUriKind.Base;
                    break;

                case Containing:
                    kind = AdvancedUriKind.Containing;
                    break;

                case Content:
                    kind = AdvancedUriKind.Content;
                    break;

                case Relative:
                    kind = AdvancedUriKind.Relative;
                    break;

                default:
                    kind = AdvancedUriKind.Indeterminate;
                    break;
            }

            return kind;
        }

        /// <summary>
        /// Returns whether or not a string contains an AdvancedUri identifier.
        /// </summary>
        /// <param name="uriString">The string to check fro an identifier.</param>
        public static bool HasIdentifier(string uriString)
        {
            uriString = uriString.Trim();

            return uriString.StartsWith(Base, StringComparison.InvariantCultureIgnoreCase) ||
                    uriString.StartsWith(Containing, StringComparison.InvariantCultureIgnoreCase) ||
                    uriString.StartsWith(Content, StringComparison.InvariantCultureIgnoreCase) ||
                    uriString.StartsWith(Relative, StringComparison.InvariantCultureIgnoreCase);
        }

        /// <summary>
        /// Reruns a string without any identifiers present in a given string.
        /// </summary>
        /// <param name="uriString">The string to remove the identfier from.</param>
        public static string RemoveIdentifier(string uriString)
        {
            uriString = uriString.Trim();

            if (HasIdentifier(uriString))
            {
                return uriString.Remove(0, 2);
            }

            return uriString;
        }

        /// <summary>
        /// Returns the identifier for a given AdvancedUriKind.
        /// </summary>
        /// <param name="kind">The UriKind to retrieve an identifier for.</param>
        public static string GetIdentifier(this AdvancedUriKind kind)
        {
            switch (kind)
            {
                case AdvancedUriKind.Base:
                    return Base;

                case AdvancedUriKind.Containing:
                    return Containing;

                case AdvancedUriKind.Content:
                    return Content;

                case AdvancedUriKind.Relative:
                    return Relative;

                default:
                    return String.Empty;
            }
        }
    }
}
